package com.mingqijia.gassafety.authority.dal.handler;

import com.mingqijia.gassafety.authority.api.req.config.QueryDivisionReqDTO;
import com.mingqijia.gassafety.authority.api.resp.config.DivisionLiteRespDTO;
import com.mingqijia.gassafety.authority.dal.command.QueryDivisionCommand;
import com.mingqijia.gassafety.db.entity.auth.DivisionEntry;
import com.mingqijia.gassafety.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
@ConditionalOnMissingBean(DivisionTranslatorImpl.class)
public interface DivisionTranslator {

    DivisionLiteRespDTO toRespDTO(DivisionEntry entry);

    QueryDivisionCommand toCommand(QueryDivisionReqDTO req);

    DivisionEntry toEntry(QueryDivisionCommand command);

    default List<DivisionLiteRespDTO> toRespDTO(List<DivisionEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toRespDTO)
                .collect(Collectors.toList());
    }
}
